package com.lujianfei.filesend.ui.server

import android.content.Context
import android.content.Intent
import android.view.MenuItem
import android.view.View
import com.lujianfei.filesend.R
import com.lujianfei.filesend.base.BaseActivity
import com.lujianfei.filesend.databinding.ActivityServerBinding
import com.lujianfei.filesend.ui.dialog.DialogButton
import com.lujianfei.filesend.ui.dialog.DialogUtils
import com.lujianfei.filesend.ui.server.viewmodel.ServerViewModel
import com.lujianfei.filetransfer.FileTransferManager
import com.lujianfei.filetransfer.TcpManager
import com.lujianfei.filetransfer.TcpMessageListener


/**
 * Author: lujianfei
 * Date: 2023/10/16 15:55
 * Description: 查找设备
 */

class ServerActivity: BaseActivity<ActivityServerBinding>(), TcpMessageListener {

    companion object {
        fun start(context: Context?) {
            context?.startActivity(Intent(context, ServerActivity::class.java))
        }
    }

    private val viewModel: ServerViewModel by lazy { buildViewModel() }
    override fun resourceId() = R.layout.activity_server

    override fun initView() {
        TcpManager.instance.addListener(this)
        binding?.vm = viewModel
    }

    override fun initData() {
        viewModel.load()
        viewModel.inputNumber.set(TcpManager.instance.getIpAddressLastDigit())
    }

    override fun initEvent() {
        binding?.onBackClick = View.OnClickListener {
            onBackPressed()
        }
        viewModel.onItemClickCallback = {
            ServerChatActivity.start(context = this, title = it.name.get()?:"", ipaddress = it.ipaddress)
        }
    }

    override fun onDestroy() {
        TcpManager.instance.removeListener(this)
        super.onDestroy()
    }

    override fun onServerStart(address: String, port: Int) {
    }

    override fun onClientAccept(address: String) {
        viewModel.onClientAccept(address)
    }

    override fun onClientClose(address: String) {
        viewModel.onClientClose(address)
    }

    override fun onMessageReceive(ip: String, message: String) {
        viewModel.onMessageReceive(ip, message)
    }

    override fun onNameAccept(address: String, name: String) {
        viewModel.onNameAccept(address, name)
    }

    override fun onConnectFailed(address: String, error: String) {

    }

    override fun onConnectSuccess(address: String) {

    }
}